/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.wj.facedetect.activity.im;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.util.CommonUtils;
import com.wj.facedetect.util.CoreHandler;
import com.wj.facedetect.util.PreferencesUtils;
import com.wj.facedetect.widget.photoview.PhotoView;


/**
 * 图片预览
 *
 * @author Jorstin Chan@容联•云通讯
 * @version 4.0
 * @date 2015-1-4
 */
public class ImagePreviewActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "ImagePreviewActivity";
    public static final int READ = 1;
    public static final int NO_READ = -1;
    public static final String FILE_PATH = "file_path";
    public static final String IS_READ = "is_read";
    private PhotoView mImageView;
    private Bitmap bitmap;
    private int isRead;
    private String filePath;
    private TextView tvLeft, tvRight;
    private CoreHandler mCoreHandler = new CoreHandler(new CoreHandler.HandlerCallbck() {
        @Override
        public boolean dispatchMessage() {
            mImageView.setImageBitmap(bitmap);
            mImageView.invalidate();
            return false;
        }
    }, false);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_preview_activity);
        Intent intent = getIntent();
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        if (intent != null) {
            isRead = intent.getIntExtra(IS_READ, -1);
            filePath = intent.getStringExtra(FILE_PATH);
            if (isRead == READ) {
                tvRight.setVisibility(View.INVISIBLE);
                tvLeft.setOnClickListener(this);
                initViewUIRead();
            } else {
                tvRight.setText("确定");
                tvRight.setVisibility(View.VISIBLE);
                tvLeft.setOnClickListener(this);
                tvRight.setOnClickListener(this);
                initViewUI();
            }
        } else {
            tvRight.setText("确定");
            tvRight.setVisibility(View.VISIBLE);
            tvLeft.setOnClickListener(this);
            tvRight.setOnClickListener(this);
            initViewUI();
        }
    }

    private void initViewUIRead() {
        if (TextUtils.isEmpty(filePath)) {
            finish();
            return;
        }
        mImageView = (PhotoView) findViewById(R.id.image);
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                bitmap = CommonUtils.getSuitableBitmap(filePath);
                mCoreHandler.sendEmptyMessageDelayed(200);
            }
        });
    }

    private void initViewUI() {
        final String path = PreferencesUtils.getInstance().getSharePreStr(PreferencesUtils.SETTINGS_CROPIMAGE_OUTPUTPATH);
        if (TextUtils.isEmpty(path)) {
            finish();
            return;
        }
        mImageView = (PhotoView) findViewById(R.id.image);
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                bitmap = CommonUtils.getSuitableBitmap(path);
                mCoreHandler.sendEmptyMessageDelayed(200);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                PreferencesUtils.getInstance().putSharePre(PreferencesUtils.SETTINGS_PREVIEW_SELECTED, false);
                finish();
                break;
            case R.id.tv_right:
                try {
                    PreferencesUtils.getInstance().putSharePre(PreferencesUtils.SETTINGS_PREVIEW_SELECTED, true);
                    setResult(RESULT_OK);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();
                break;
            default:
                break;
        }
    }

}
